import {Api} from './Api';
import axios from 'axios';
import {Message} from 'element-ui';
// Post请求
const Service = {
    doPost(url, data, showError = true) {
        return new Promise((resolve, reject) => {
            axios.post(url, data).then(response => {
                if (response.status) {
                    let data = response.data;
                    if (data.status) {
                        resolve(data.data);
                    } else {
                        if (showError) {
                            Message.warning(data.data);
                        }
                        reject({
                            message: data.data,
                            url: url
                        });
                    }
                }
            }).catch(error => {
                if (error.response) {
                    let data = error.response.data;
                    let message = error.response.data.message;
                    let timestamp = data.timestamp;
                    if (message) {
                        if (showError) {
                            Message.warning(message);
                        }
                        reject({
                            message: message,
                            url,
                            timestamp
                        });
                    } else {
                        reject({
                            message: `${data.status}: ${data.error}`,
                            url,
                            timestamp
                        });
                    }
                } else {
                    reject({
                        url,
                        message: error.message
                    });
                }
            })
        })
    }
    // doPost(url, data) {
    //     return new Promise((resolve, reject) => {
    //         axios.post(url, data).then(response => {
    //             console.log(response);
    //         }).catch(err => {
    //             console.log(err)
    //         })
    //     })
    // },
    // 暂不使用
    // doGet(url, data) {
    //     return new Promise((resolve, reject) => {
    //         axios.get(url, {
    //             params: data
    //         }).then(response => {
    //             if (response.status === 200) {
    //                 resolve(response.data)
    //             } else {
    //                 console.log("状态错误，暂不处理")
    //             }
    //         }).catch(error => {
    //             console.log(error)
    //         })
    //     })
    // }
};
Service.utils = {};
Service.base = {

    // -------------------------------------登录注册相关--------------------------------------
    login(userName, password) {
        return Service.doPost(Api.baseUrl + Api.LoginOrRegister.login, {
            userName, password
        })
    },
    checkName(userName) {
        return Service.doPost(Api.baseUrl + Api.LoginOrRegister.checkName, {
            userName
        })
    },
    register(userName, password) {
        return Service.doPost(Api.baseUrl + Api.LoginOrRegister.register, {
            userName, password
        })
    },
    // -------------------------------------用户设置相关--------------------------------------
    updatePic(input, select, user_id) {
        return Service.doPost(Api.baseUrl + Api.UserSetting.PageSetting.updatePic, {
            input, select, user_id
        })
    },
    searchUsers() {
        return Service.doPost(Api.baseUrl + Api.UserSetting.RoleManage.searchUsers);
    },
    delUser(user_id) {
        return Service.doPost(Api.baseUrl + Api.UserSetting.RoleManage.delUser, {
            user_id
        })
    },
    batchDeleteUsers(ids) {
        return Service.doPost(Api.baseUrl + Api.UserSetting.RoleManage.batchDelete, {
            ids
        })
    },
    // -------------------------------------个人中心照片墙相关--------------------------------------
    loadPhoto(user_id) {
        return Service.doPost(Api.baseUrl + Api.PersonalCenter.PhotoWall.loadPhoto, {
            user_id
        });
    },
    // -------------------------------------测试--------------------------------------
    test() {
        return Service.doPost(Api.baseUrl + Api.test)
    }
};
export default Service;